import { Injectable } from '@nestjs/common';
import { CreateOrderDto } from './dto/create-order.dto';
import { UpdateOrderDto } from './dto/update-order.dto';
import { InjectModel } from '@nestjs/mongoose';
import { Order } from './schemas/order.schema';
import { Model } from 'mongoose';

@Injectable()
export class OrderService {
  constructor(@InjectModel(Order.name) private orderModel: Model<Order>) { }

  //  添加订单
  async create(createOrderDto: CreateOrderDto) {
    await this.orderModel.create(createOrderDto);
    return {
      code: 200,
      msg: "添加成功！",
      data: {}
    }
  }

  // 获取图文问诊订单
  async findList(paymentType: string) {
    if (paymentType === "图文问诊" || paymentType === "开药问诊") {
      const data = await this.orderModel.find({ paymentType })
      if (data.length > 0) {
        return {
          code: 200,
          msg: "查询成功！",
          data
        }
      } else {
        return {
          code: 200,
          msg: "暂无订单数据！"
        }
      }
    } else {
      return {
        code: 200,
        msg: "入参不合法！"
      }
    }
  }

  // 根据订单号获取图文问诊订单
  async findOne(OrderNumber: string) {
    const data = await this.orderModel.findOne({ OrderNumber })
    return {
      code: 200,
      msg: "查询成功！",
      data
    }
  }

  async update(id: string, updateOrderDto: UpdateOrderDto) {
    if (updateOrderDto.PaymentIsDone !== "已完成" && updateOrderDto.PaymentIsDone !== "待支付" && updateOrderDto.PaymentIsDone !== "已取消" && updateOrderDto.PaymentIsDone !== "待接诊" && updateOrderDto.PaymentIsDone !== "咨询中") {
      return {
        code: 200,
        msg: "入参不合法！"
      }
    } else {
      await this.orderModel.findOneAndUpdate({ OrderNumber: id }, updateOrderDto);
      return {
        code: 200,
        msg: "修改成功！",
        data: {}
      }
    }
  }

  remove(id: number) {
    return `This action removes a #${id} order`;
  }
}
